<?php

class SetupDAL extends DefaultDAL {

	protected static $objClassName =  'Setup';
	protected static $objClassTable = 'mf_setup';
	protected static $objClassTablePK = 'id_mf_setup';
	private static $optClass;

	/**
	 * Zapisuje warto�� zmiennej w tabeli Setup
	 *
	 * @param String $zmienna
	 * @param String $wartosc
	 * @param String $opis
	 */
	public static function SaveVariable($zmienna, $wartosc, $opis) {
		$db = Registry::Get('db');
		$sql = "REPLACE INTO mf_setup (variable, value, description) values ( :1, :2, :3)";
		$stmt = $db->prepare($sql)
						->bindParam(1, $zmienna)
						->bindParam(2, $wartosc)
						->bindParam(3, $opis)
						->execute();

		//		return $stmt->GetInsertionId();
	}

	/**
	 * Pobiera z tabeli setup zmienne oraz rejestruje je w Registry
	 *
	 * @param boolean $set_registry
	 * @return Array
	 */
	public static function GetAllVariables($set_registry=true) {
		$db = Registry::Get('db');

		if ($db instanceof DBMysql)
			$sql = "select variable,value,description from mf_setup order by variable,value";


		$stmt = $db->execute($sql);
		$stmt->CacheStart(600);
		$array = $stmt->FetchAllAssoc();
		
		$returnArray = array();
		foreach ($array as $row) {

			$row["value_str"] = $row["value"];
			if ($row["value"] == "false") {
				$row["value"] = false;
			}
			if ($row["value"] == "true") {
				$row["value"] = true;
			}

			if ($set_registry) {
				Registry::Set($row["variable"], $row["value"]);
			}

			$tmpArray["variable"] = $row["variable"];
			$tmpArray["value"] = $row["value_str"];
			$tmpArray["description"] = $row["description"];

			$returnArray[] = $tmpArray;
		}
		return $returnArray;
	}

	/**
	 * ODczytuje z tabeli mf_setup zmienn� o danej nazwie
	 *
	 * @param String $variable
	 * @return String
	 */
	public static function GetVariableByName($variable) {
		$db = Registry::Get('db');
		$sql = "select variable,value,description from mf_setup where variable=:1";
		$stmt = $db->prepare($sql)
						->bindParam(1, $variable)
						->execute();
		$array = $stmt->fetchAllAssoc();

		return $array[0];
	}

	/**
	 *
	 * @return DalData $dalData
	 */
	public static function GetDalDataObj($params = array()) {

		$dalData = new DalData($params);
		$dalData->setObjClassName(self::GetObjClassName());
		$dalData->setObjClassTable(self::GetObjClassTable());
		$dalData->setObjClassTablePK(self::GetObjClassTablePK());
		$dalData->setOptClass(self::GetOptClass());

		return $dalData;
	}


	public static function GetObjClassName() {
		if(self::$objClassName != '') {
			$class = self::$objClassName;
		} else {
			$class = str_replace('DAL', '', __CLASS__);
		}

		return $class;
	}

	public static function GetOptClass() {
		if(self::$optClass!=null) {
			return self::$optClass;
		} else {
			return self::GetObjClassName();
		}
	}

	public static function GetObjClassTablePK() {
		if(self::$objClassTablePK != '') {
			$return = self::$objClassTablePK;
		} else {
			$class = self::GetObjClassName();
			$classObj = new $class();
			$return = $classObj->GetClassTablePK();
		}
		return $return;
	}

	public static function GetObjClassTable() {
		if(self::$objClassTable!='') {
			$return = self::$objClassTable;
		} else {
			$class = self::GetObjClassName();
			$classObj = new $class();
			$return = $classObj->GetTableName();
		}

		return $return;
	}

	public static function CheckVariableUnique($variableName) {
		$dalData = self::GetDalDataObj();
		$dalData->addCondition('variable', $variableName);
		$dalData->setLimit(1);

		return self::GetObj($dalData);
	}

}

?>